import pathlib
import sys

from bqgraph.kline_helper import KLineWidget
from bqgraph.qt import Qt, QApplication, QtGui, QtWidgets, QMainWindow

from bqgraph.ui_ta import Ui_MainWindow
from bqgraph.tick_widget import TickWidget

from ds.redis_event_engine import RedisEventEngine
from ds.get_main_instruments import get_main_instruments, get_market_instruments, rc
from bqgraph.market_monitor import create_market_monitor
from bqgraph.functions import (
    get_ss_instruments,
    save_ss_instruments,
    get_trade_instruments,
)  # ,save_trade_instruments


class MyChart(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MyChart, self).__init__()
        self.main_instruments = get_main_instruments()
        self.market_instruments = get_market_instruments()
        self.selected_instruments = get_ss_instruments()
        self.trade_instruments = get_trade_instruments()
        self.data_engine = RedisEventEngine(rds=rc)
        self.bar_widget = None
        self.tick_widget = None
        self.iid = "iid"
        self.setupUi(self)
        self.iniUI()

    def iniUI(self):
        # self.dwQuoteDetail.setWindowFlags(Qt.FramelessWindowHint)
        self.dwQuoteTotal.setWindowFlags(Qt.FramelessWindowHint)
        self.tick_widget = TickWidget()
        self.bar_widget = KLineWidget(parent=self.centralWidget())
        # self.bar_widget.showMaximized()
        self.mainLayout.addWidget(self.bar_widget)
        self.twTickDetail.setMaximumWidth(self.dwQuoteTotal.maximumWidth() - 1)
        self.twTickDetail.setSpan(0, 0, 1, 2)  # 合并第一行的1-2两列
        self.twTickDetail.setSpan(0, 2, 1, 2)  # 合并第一行的3-4两列
        self.instrument_info_btn = QtWidgets.QTableWidgetItem("资讯")
        self.twTickDetail.setItem(0, 2, self.instrument_info_btn)
        self.twTickDetail.horizontalHeader().resizeSections(
            QtWidgets.QHeaderView.ResizeMode.ResizeToContents
        )

        self.actLoginQuoteServer.triggered.connect(self.login_quote)

        self.login_quote()

    def on_iid_changed(self, event):
        iid = event.data
        self.iid = iid
        self.twTickDetail.setItem(0, 0, QtWidgets.QTableWidgetItem(iid))

    def on_tick(self, event):
        tick = event.data
        print(tick)
        self.twTickDetail.setItem(1, 1, QtWidgets.QTableWidgetItem(tick["AskPrice1"]))
        self.twTickDetail.setItem(1, 2, QtWidgets.QTableWidgetItem(tick["AskVolume1"]))
        self.twTickDetail.setItem(2, 1, QtWidgets.QTableWidgetItem(tick["BidPrice1"]))
        self.twTickDetail.setItem(2, 2, QtWidgets.QTableWidgetItem(tick["BidVolume1"]))

    def login_quote(self):
        create_market_monitor(
            self.twQuote,
            self.data_engine,
            self.main_instruments,
            self.market_instruments,
            self.selected_instruments,
            self.trade_instruments,
            self,
        )
        self.mainTable.iid_changed_signal.connect(self.on_iid_changed)
        self.SSTable.iid_changed_signal.connect(self.on_iid_changed)
        self.CZCETable.iid_changed_signal.connect(self.on_iid_changed)
        self.DCETable.iid_changed_signal.connect(self.on_iid_changed)
        self.SHFETable.iid_changed_signal.connect(self.on_iid_changed)
        self.CFFEXTable.iid_changed_signal.connect(self.on_iid_changed)
        self.TradingTable.iid_changed_signal.connect(self.on_iid_changed)
        self.mainTable.tick_signal.connect(self.on_tick)
        self.SSTable.tick_signal.connect(self.on_tick)
        self.CZCETable.tick_signal.connect(self.on_tick)
        self.DCETable.tick_signal.connect(self.on_tick)
        self.SHFETable.tick_signal.connect(self.on_tick)
        self.CFFEXTable.tick_signal.connect(self.on_tick)

        # self.dwLeft.hide()


if __name__ == "__main__":
    import sys

    app = QApplication()
    window = MyChart()
    window.show()
    sys.exit(app.exec())
